import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_extension.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/utils/xfg_utils.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// Descriptions: 取件时间
/// User: tangzhentao
/// Date: 4:58 下午 2021/12/7
///

Future<String?> showPickupTimeDialog() async {
  return await showModalBottomSheet<String>(
      context: XfgNav.currentContext,
      builder: (_) => _PickupTimeDialog()
  );
}

class _PickupTimeDialog extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PickupTimeState();
}

class _PickupTimeState extends State<_PickupTimeDialog> {
  late final List<ItemData> _dateList;

  late ItemData _itemData;
  late String _time;

  @override
  void initState() {
    _dateList = _getData();
    _itemData = _dateList[0];
    _time = _itemData.amList[0];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: XfgSize().size556,
      decoration: BoxDecoration(color: XfgColor.white, borderRadius: BorderRadius.vertical(top: Radius.circular(XfgSize().size16))),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.symmetric(horizontal: XfgSize().size20),
            margin: EdgeInsets.only(bottom: XfgSize().size24),
            height: XfgSize().size45,
            alignment: Alignment.bottomCenter,
            child: XfgText('您什么时间有空？', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold),
          ),
          Expanded(
            child: Row(
              children: [
                SizedBox(
                  width: XfgSize().size164,
                  child: SingleChildScrollView(
                    child: Column(
                      children: _dateList.map((item) {
                        final select = _itemData == item;
                        return InkWell(
                          onTap: () {
                            if (!select) {
                              setState(() {
                                _itemData = item;
                                _time = item.amList[0];
                              });
                            }
                          },
                          child: Container(
                            color: select ? XfgColor.white:XfgColor.background,
                            height: XfgSize().size48,
                            alignment: Alignment.center,
                            child: XfgText(item.date, fontSize: XfgSize().text16, color: select ? XfgColor.text1D:XfgColor.text3),
                          ),
                        );
                      }).toList(),
                    )
                  ),
                ),
                Expanded(
                  child: SingleChildScrollView(
                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size16),
                      child: Column(
                        children: [
                          Visibility(
                            visible: _itemData.amList.isNotEmpty,
                            child: Container(
                              alignment: Alignment.centerLeft,
                              child: XfgText('上午', fontSize: XfgSize().text16, color: XfgColor.text2),
                            ),
                          ),
                          ..._itemData.amList.map((time) {
                            final select = _time == time;
                            return Ink(
                              decoration: bottomBorderDecoration,
                              height: XfgSize().size48,
                              child: InkWell(
                                onTap: () {
                                  if (!select) {
                                    setState(() => _time = time);
                                  }
                                },
                                child: Container(
                                  decoration: bottomBorderDecoration,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: [XfgText(time, fontSize: XfgSize().text14, color: select ? XfgColor.theme:XfgColor.text2)],
                                  ),
                                ),
                              ),
                            );
                          }),
                          Visibility(
                            visible: _itemData.pmList.isNotEmpty,
                            child: Container(
                              height: XfgSize().size32,
                              alignment: Alignment.bottomLeft,
                              child: XfgText('下午', fontSize: XfgSize().text16, color: XfgColor.text2),
                            ),
                          ),
                          ..._itemData.pmList.map((time) {
                            final select = _time == time;
                            return InkWell(
                              onTap: () {
                                if (!select) {
                                  setState(() => _time = time);
                                }
                              },
                              child: Container(
                                height: XfgSize().size48,
                                alignment: Alignment.centerLeft,
                                child: XfgText(time, fontSize: XfgSize().text14, color: select ? XfgColor.theme:XfgColor.text2),
                              ),
                            );
                          })
                        ],
                      )
                  ),
                )
              ],
            ),
          ),
          SizedBox(height: XfgSize().size10),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(
                  onPressed: XfgNav.pop,
                  style: xfgButtonStyle(Size(XfgSize().size175, XfgSize().size48),
                      radius: XfgSize().size48,
                      side: BorderSide(color: XfgColor.colorE, width: XfgSize().size0_5),
                      color: XfgColor.transparent
                  ),
                  child: XfgText('取消', fontSize: XfgSize().text18, color: XfgColor.theme)
              ),
              SizedBox(width: XfgSize().size10),
              TextButton(
                  onPressed: () => XfgNav.pop('${_itemData.yyyyMMDD} $_time'),
                  style: xfgButtonStyle(Size(XfgSize().size175, XfgSize().size48),
                    radius: XfgSize().size48,
                    side: BorderSide(color: XfgColor.colorE, width: XfgSize().size0_5),
                  ),
                  child: XfgText('确认', fontSize: XfgSize().text18, color: XfgColor.white)
              )
            ],
          ),
          bottomNavigatorWidget,
        ],
      ),
    );
  }

  /// 获取数据
  List<ItemData> _getData() {
    final DateTime now = DateTime.now();
    return List.generate(8, (index) {
      DateTime add = now.add(Duration(days: index + 1));
      final date = add.format(formats: [mm, '月', dd, '日']);
      final weekDay = index == 0 ? '明天':getWeekday(add.weekday);
      final yyyyMMDD = add.format();
      return ItemData('$date($weekDay)', yyyyMMDD, ['08:00-10:00', '10:00-12:00'], ['12:00-14:00', '14:00-16:00', '16:00-18:00']);
    });
  }
}

/// 日期
class ItemData {
  final String date;
  final String yyyyMMDD;
  final List<String> amList;
  final List<String> pmList;

  ItemData(this.date, this.yyyyMMDD, this.amList, this.pmList);
}